//
//  SEEmptyDataSet.swift
//  SmartEyeForDoctor
//
//  Created by QDSG on 2021/5/27.
//

import Foundation
import EmptyDataSet_Swift

extension UIScrollView {
    
    private struct AssociatedEmptyKeys {
        static var emptyKey: Void?
    }
    
    var se_empty: SEEmptyView? {
        get { objc_getAssociatedObject(self, &AssociatedEmptyKeys.emptyKey) as? SEEmptyView }
        set {
            self.emptyDataSetDelegate = newValue
            self.emptyDataSetSource = newValue
            objc_setAssociatedObject(self, &AssociatedEmptyKeys.emptyKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}

class SEEmptyView: EmptyDataSetSource, EmptyDataSetDelegate {
    var image: UIImage?
    
    var allowShow: Bool = false
    var verticalOffset: CGFloat = 0.0
    
    private var tapClosure: (() -> Void)?
    
    convenience init(image: UIImage? = Asset.emptyData.image, verticalOffset: CGFloat = 0.0, tapClosure: (() -> Void)?) {
        self.init()
        self.image = image
        self.verticalOffset = verticalOffset
        self.tapClosure = tapClosure
    }
    
    func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        return NSAttributedString(string: "暂无数据")
    }
    
    func verticalOffset(forEmptyDataSet scrollView: UIScrollView) -> CGFloat {
        return verticalOffset
    }
    
    func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
        return image
    }
    
    func spaceHeight(forEmptyDataSet scrollView: UIScrollView) -> CGFloat {
        return 5
    }
    
    func emptyDataSetShouldDisplay(_ scrollView: UIScrollView) -> Bool {
        return allowShow
    }
    
    func emptyDataSet(_ scrollView: UIScrollView, didTapView view: UIView) {
        tapClosure?()
    }
}
